package com.example.reschain;

import java.util.concurrent.BrokenBarrierException;
import java.util.concurrent.CyclicBarrier;

/**
 * Created by jay.zhou on 2018/9/4.
 */
public class CyclicBarrierDemo {

    private static final int THREAD_COUNT_NUM = 4;

    public static void main(String[] args) {
        // 创建一个Runnable接口实现
        Runnable task = () -> System.out.println("执行指定任务");
        // 创建一个CyclicBarrier
        CyclicBarrier barrier = new CyclicBarrier(THREAD_COUNT_NUM , task);

        for (int i = 0; i < THREAD_COUNT_NUM; i++) {
            new Thread(() -> {
                System.out.println(Thread.currentThread().getName() + "执行任务完毕");
                //我任务完成了,所以我等待了
                try {
                    barrier.await();
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            }, "Thread" + i).start();
        }

    }
}
